﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CommandLineHandlerTest.cs" company="Matthias Friedrich">
//   Copyright © Matthias Friedrich 2013
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CommandLineHandling.UnitTest
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using CommandlineHandling;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class CommandLineHandlerTest
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        public void CommandLineHandlerConstructorTest()
        {
            var target = new CommandLineHandler();

            Assert.IsNotNull(target.Mapper);
        }

        [TestMethod]
        public void ExecuteTest()
        {
            var target = new CommandLineHandler();

            bool executed = false;

            Func<ICommandLineOption, ICommandLineOption, object> executeHandler = (caller, option) =>
                {
                    executed = true;
                    return null;
                };

            var argument = new Option() { Execute = executeHandler };

            IEnumerable<ICommandLineOption> arguments = new List<ICommandLineOption>() { argument };

            target.Execute(arguments);

            Assert.IsTrue(executed);
        }

        [TestMethod]
        public void IsOptionTypeTest()
        {
            Type t = typeof(TestOption);

            const bool Expected = true;

            bool actual = CommandLineHandler.IsOptionType(t);

            Assert.AreEqual(Expected, actual);
        }

        [TestMethod]
        public void ParseTest()
        {
            var target = new CommandLineHandler();

            IEnumerable<string> args = new[] { "/x", "-a", "-b" };

            IEnumerable<ICommandLineOption> actual = target.Parse(args);

            // 	Assert
            Assert.AreEqual(1, actual.Count());

            ICommandLineOption option = actual.First();

            Assert.AreEqual("x", option.Name);

            Assert.IsTrue(option.Parameters.Any((x) => x.Name == "a"));

            Assert.IsTrue(option.Parameters.Any((x) => x.Name == "b"));
        }

        [TestMethod]
        public void RegisterTest()
        {
            IEnumerable<string> args = new[] { "/x" };

            var handler = new CommandLineHandler();

            handler.Register<TestOption>("x");

            ICommandLineOption option = handler.Parse(args).First();

            Assert.IsInstanceOfType(option, typeof(TestOption));
        }
    }
}